<?php
	class parse
	{
		//comperr!n=Me@unaffiliated/comperr

		private $msg;
		private $parsed;
		private $userCmd;
	
		/*
			Parsed 	[0] == Server
				[1] == Command
				[2] == name
				[3] == message
		*/
		
		public function setText($msg)
		{
			$this->msg = $msg;
			$this->parseRFC();
			$this->parseUser();
		}
	
		protected function parseRFC()
		{
			$result = explode(' ',$this->msg,4);
			$result[0] = $this->ircTrim($result[0]);
			if (isset($result[3]))
			{
				$result[3] = $this->ircTrim($result[3]);
			}
			$this->parsed = $result;
		}

		protected function parseUser()
		{
			if (isset($this->parsed[3]))
			{
				$result = explode(' ',$this->parsed[3],3);
				$result[0] = $this->ircTrim($result[0]);
				$this->userCmd = $result;
			}
		}

		protected static function ircTrim($txt)
		{
			return ltrim(trim($txt),':');
		}


		/* make a function to print to the CLI & do something! */

		/* get the parse info */
		public function getServer()
		{
			return $this->parsed[0];
		}

		public function getCmd()
		{
			return $this->parsed[1];
		}

		public function getName()
		{
			return $this->parsed[2];
		}

		public function getUserCmd()
		{
			return $this->userCmd[0];
		}
		
		// skipping flags - use checkFlag instead //
		
		public function checkFlag($flag)
		{
			if (strpos($flag,$this->userCmd[1])===false)
			{
				return true;
			}
			return false;
		}

		public function getUserMsg()
		{
			return $this->userCmd[2];
		}

		public function getUserData()
		{
//			  :G0D!comperr @BigBrother.edu 
			$user = $this->parsed[0];
			if (strpos($user,'@')>0)
			{
				$user = ltrim($user,':');
				$tmp = explode('@',$user);
				$HOST = $tmp[1];
				$tmp = explode('!',$tmp[0]);
				$NICK = $tmp[0];
				$IDENT = $tmp[1];
				return array($NICK,$IDENT,$HOST);
			}
			return array('','','');
		}

		/* special functions --> this really should be done outside of the class but w/e */
		public function getPingBack()
		{
			$arr = $this->parsed;
			$arr[0] = null;
			return (implode (' ',$arr));
		}
	}
?>
